package com.zsj.juc.Atomic;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author zsj
 * @version 1.0
 * @date 2024/5/17 14:06
 * @description
 */
class Number {
    AtomicInteger atomicInteger = new AtomicInteger();

    public void pp() {
        atomicInteger.getAndIncrement();//++操作
    }
}

@Slf4j
public class AtomicIntegerDemo {
    public static final int SIZE = 50;

    private static final CountDownLatch countDownLatch = new CountDownLatch(SIZE);

    public static void main(String[] args) throws InterruptedException {
        Number number = new Number();
        for (int i = 0; i < SIZE; i++) {
            new Thread(() -> {
                try {
                    for (int j = 0; j < 1000; j++) {
                        number.pp();
                    }
                } finally {
                    countDownLatch.countDown();
                }
            }, "" + i).start();
        }
        countDownLatch.await();
        log.info("{}\tresult{}", Thread.currentThread().getName(), number.atomicInteger.get());
    }
}
